var gulp = require('gulp');
var path = require('path');
var fs = require('fs');
var clean = require('gulp-rimraf');
var less = require('gulp-less');
var minifyCSS = require('gulp-minify-css');
var LessPluginCleanCSS = require('less-plugin-clean-css');
var cleancss = new LessPluginCleanCSS({
    advanced: true
});
var ejs = require('gulp-ejs');
var htmlmin = require('gulp-htmlmin');
var rename = require('gulp-rename');
var concat = require('gulp-concat');
var connect = require('gulp-connect');
var watch = require('gulp-watch');
var copy = require('gulp-contrib-copy');
var uglify = require('gulp-uglify');
var md5 = require('md5');
var version = md5(new Date().getTime());

gulp.task('clean', function() {
    return gulp.src(['dest/'], {
            read: false
        })
        .pipe(clean());
});

gulp.task('html', function() {
    return gulp.src(['resource/*.html'])
        .pipe(htmlmin({
            collapseWhitespace: true,
            removeComments: true
        }))
        .pipe(gulp.dest('dest/'))
        .pipe(connect.reload());
});

gulp.task('less', function() {
    return gulp.src('resource/style.less')
        .pipe(less({
            plugins: [cleancss],
            paths: [path.join(__dirname, 'less', 'includes')]
        }))
        .pipe(minifyCSS())
        .pipe(gulp.dest('dest/'))
        .pipe(connect.reload());
});


gulp.task('copyScript', function() {
    gulp.src(['resource/date.js', 'resource/page.js', 'resource/avalon.mobile.min.js','resource/zepto.min.js','resource/sha1.js'])
        .pipe(gulp.dest('dest/'))
        .pipe(connect.reload());
    gulp.src('resource/*.jpg')
        .pipe(gulp.dest('dest/'));
});

gulp.task('connect', function() {
    connect.server({
        root: 'dest/',
        port: 3030,
        livereload: {
            port: 35358
        }
    });
});

gulp.task('watch', function() {
    gulp.watch('resource/*.less', ['less']);
    gulp.watch(['resource/date*.js','resource/page.js'], ['copyScript']);
    gulp.watch(['resource/*.html'], ['html']);
});
gulp.task('default', ['clean'], function() {
    // 将你的默认的任务代码放在这
    gulp.start(['html', 'less', 'copyScript', 'watch', 'connect']);
});
